﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.CodeDom;
using NHibernate.Cfg.MappingSchema;

namespace MVCGen.Generators.Entity {
	public class HbmIdToPropertyGenerator : ICodeTypeMemberGenerator {
		private HbmId id;

		public HbmIdToPropertyGenerator(HbmId id) {
			this.id = id;
		}

		public CodeTypeMember Generate() {
			CodeMemberProperty result = new CodeMemberProperty() {
				Name = id.name,
				Type = new CodeTypeReference(id.type),
				Attributes = MemberAttributes.Public | MemberAttributes.VTableMask,
				HasGet = true,
				HasSet = true
			};
			result.GetStatements.Add(
				new CodeMethodReturnStatement(
					new CodeFieldReferenceExpression(
						new CodeThisReferenceExpression(), id.name.MakeFieldName())
					)
				);
			result.SetStatements.Add(
				new CodeAssignStatement(
					new CodeFieldReferenceExpression(
						new CodeThisReferenceExpression(), id.name.MakeFieldName()
					), 
					new CodePropertySetValueReferenceExpression()
					)
				);
			return result;
		}
	}
}
